package org.m2acsi.entities;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.Size;
import org.m2acsi.utils.PrettyDate;

@Entity(name = "Opinions")
public class Opinion implements Serializable {

    private Long id;
    private String comment;
    private User user;
    private Task task;
    private Document document;
    private Date creationDate;
    private boolean isNew;

    public Opinion() {
        this.creationDate = new Date();
    }

    public Opinion(String comment) {
        this();
        this.comment = comment;
    }

    public Opinion(String comment, Document document) {
        this(comment);
        this.document = document;
    }

    public Opinion(String comment, User user, Task task) {
        this(comment);
        this.user = user;
        this.task = task;
    }

    public Opinion(String comment, User user, Task task, Document document) {
        this(comment, user, task);
        this.document = document;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Size(min = 1, max = 2000)
    @Column(nullable = false, updatable = false, length = 2000)
    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = true)
    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }

    @ManyToOne
    @JoinColumn(name = "USER_ID", nullable = false, updatable = false)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @ManyToOne
    @JoinColumn(name = "TASK_ID", nullable = false, updatable = false)
    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
    }

    @Temporal(TemporalType.DATE)
    @Column(nullable = false, updatable = false)
    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    @Override
    public String toString() {
        return "Opinion{" + "id=" + id + ", comment=" + comment + ", creationDate=" + creationDate + " UserID " + user.getId() + " TaskID " + task.getId() + " DocumentID " + document.getId() + '}';
    }

    public String getPrettyCreationDate() {
        return PrettyDate.niceCreation(creationDate);
    }

    public boolean getHasAttachment() {
        return document != null;
    }

    @Transient
    public boolean getIsNew() {
        return isNew;
    }

    public void setIsNew(boolean isNew) {
        this.isNew = isNew;
    }

    public String getNewLabel() {
        return isNew ? "new" : "";
    }
}
